const { AVATAR_UPLOAD_PATH } = require("../config/path");
const fileService = require("../service/file.service");
const userService = require("../service/user.service");
const fs = require('fs')

class UserController {
  async create(ctx, next) {
    const user = ctx.request.body
    // 将请求参数传递给数据库操作中间件
    let res = await userService.create(user)
    ctx.body = {
      msg: '创建用户成功',
      data: res
    }
  }

  async getAvatar(ctx, next) {

    const { userId } = ctx.params

    // 查询avatar数据库
    let fileInfo = await fileService.queryAvatarByUserId(userId)

    // 根据文件信息res，读取保存在文件夹中的图片
    const { filename, mimetype } = fileInfo
    ctx.type = mimetype
    ctx.body = fs.createReadStream(`${AVATAR_UPLOAD_PATH}/${filename}`)
  }
}

module.exports = new UserController()